1
|
|
View Code Duplication |
var TINY={};
|
|
|
|
|
2
|
|
|
|
3
|
|
|
function T$(i){return document.getElementById(i)}
|
4
|
|
|
function T$$(e,p){return p.getElementsByTagName(e)}
|
5
|
|
|
|
6
|
|
|
TINY.table=function(){
|
7
|
|
|
function sorter(n,t,p){this.n=n; this.id=t; this.p=p; if(this.p.init){this.init()}}
|
8
|
|
|
sorter.prototype.init=function(){
|
9
|
|
|
this.set(); var t=this.t, i=d=0; t.h=T$$('tr',t)[0];
|
|
|
|
|
10
|
|
|
t.l=t.r.length; t.w=t.r[0].cells.length; t.a=[]; t.c=[]; this.p.is=this.p.size;
|
11
|
|
|
if(this.p.colddid){
|
12
|
|
|
d=T$(this.p.colddid);
|
13
|
|
|
var o=document.createElement('option'); o.value=-1; o.innerHTML='All Colomns'; d.appendChild(o)
|
14
|
|
|
}
|
15
|
|
|
for(i;i<t.w;i++){
|
16
|
|
|
var c=t.h.cells[i]; t.c[i]={};
|
17
|
|
|
if(c.className!='nosort'){
|
18
|
|
|
c.className=this.p.headclass; c.onclick=new Function(this.n+'.sort('+i+')');
|
|
|
|
|
19
|
|
|
c.onmousedown=function(){return false};
|
20
|
|
|
}
|
21
|
|
|
if(this.p.columns){
|
22
|
|
|
var l=this.p.columns.length, x=0;
|
|
|
|
|
23
|
|
|
for(x;x<l;x++){
|
24
|
|
|
if(this.p.columns[x].index==i){
|
25
|
|
|
var g=this.p.columns[x];
|
26
|
|
|
t.c[i].format=g.format==null?1:g.format; t.c[i].decimals=g.decimals==null?2:g.decimals
|
27
|
|
|
}
|
28
|
|
|
}
|
29
|
|
|
}
|
30
|
|
|
if(d){
|
31
|
|
|
var o=document.createElement('option'); o.value=i; o.innerHTML=T$$('h3',c)[0].innerHTML; d.appendChild(o)
|
|
|
|
|
32
|
|
|
}
|
33
|
|
|
}
|
34
|
|
|
this.reset()
|
35
|
|
|
};
|
36
|
|
|
sorter.prototype.reset=function(){
|
37
|
|
|
var t=this.t; t.t=t.l;
|
38
|
|
|
for(var i=0;i<t.l;i++){t.a[i]={}; t.a[i].s=1}
|
39
|
|
|
if(this.p.sortcolumn!=undefined){
|
40
|
|
|
this.sort(this.p.sortcolumn,1,this.p.is)
|
41
|
|
|
}else{
|
42
|
|
|
if(this.p.paginate){this.size()} this.alt(); this.sethover()
|
43
|
|
|
}
|
44
|
|
|
this.calc()
|
45
|
|
|
};
|
46
|
|
|
sorter.prototype.sort=function(x,f,z){
|
47
|
|
|
var t=this.t; t.y=x; var x=t.h.cells[t.y], i=0, n=document.createElement('tbody');
|
48
|
|
|
for(i;i<t.l;i++){
|
49
|
|
|
t.a[i].o=i; var v=t.r[i].cells[t.y]; t.r[i].style.display='';
|
50
|
|
|
while(v.hasChildNodes()){v=v.firstChild}
|
51
|
|
|
t.a[i].v=v.nodeValue?v.nodeValue:''
|
52
|
|
|
}
|
53
|
|
|
for(i=0;i<t.w;i++){var c=t.h.cells[i]; if(c.className!='nosort'){c.className=this.p.headclass}}
|
54
|
|
|
if(t.p==t.y&&!f){t.a.reverse(); x.className=t.d?this.p.ascclass:this.p.descclass; t.d=t.d?0:1}
|
55
|
|
|
else{t.p=t.y; f&&this.p.sortdir==-1?t.a.sort(cp).reverse():t.a.sort(cp); t.d=0; x.className=this.p.ascclass}
|
56
|
|
|
for(i=0;i<t.l;i++){var r=t.r[t.a[i].o].cloneNode(true); n.appendChild(r)}
|
57
|
|
|
t.replaceChild(n,t.b); this.set(); this.alt(); if(this.p.paginate){this.size(z)} this.sethover()
|
58
|
|
|
};
|
59
|
|
|
sorter.prototype.sethover=function(){
|
60
|
|
|
if(this.p.hoverid){
|
61
|
|
|
for(var i=0;i<this.t.l;i++){
|
62
|
|
|
var r=this.t.r[i];
|
63
|
|
|
r.setAttribute('onmouseover',this.n+'.hover('+i+',1)');
|
64
|
|
|
r.setAttribute('onmouseout',this.n+'.hover('+i+',0)')
|
65
|
|
|
}
|
66
|
|
|
}
|
67
|
|
|
};
|
68
|
|
|
sorter.prototype.calc=function(){
|
69
|
|
|
if(this.p.sum||this.p.avg){
|
70
|
|
|
var t=this.t, i=x=0, f,r;
|
|
|
|
|
71
|
|
|
if(!T$$('tfoot',t)[0]){
|
72
|
|
|
f=document.createElement('tfoot'); t.appendChild(f)
|
73
|
|
|
}else{
|
74
|
|
|
f=T$$('tfoot',t)[0]; while(f.hasChildNodes()){f.removeChild(f.firstChild)}
|
75
|
|
|
}
|
76
|
|
|
if(this.p.sum){
|
77
|
|
|
r=this.newrow(f);
|
78
|
|
|
for(i;i<t.w;i++){
|
79
|
|
|
var j=r.cells[i];
|
|
|
|
|
80
|
|
|
if(this.p.sum.exists(i)){
|
81
|
|
|
var s=0, m=t.c[i].format||'';
|
82
|
|
|
for(x=0;x<this.t.l;x++){
|
83
|
|
|
if(t.a[x].s){s+=parseFloat(t.r[x].cells[i].innerHTML.replace(/(\$|\,)/g,''))}
|
84
|
|
|
}
|
85
|
|
|
s=decimals(s,t.c[i].decimals?t.c[i].decimals:2);
|
86
|
|
|
s=isNaN(s)?'n/a':m=='$'?s=s.currency(t.c[i].decimals):s+m;
|
|
|
|
|
87
|
|
|
r.cells[i].innerHTML=s
|
88
|
|
|
}else{r.cells[i].innerHTML=' '}
|
89
|
|
|
}
|
90
|
|
|
}
|
91
|
|
|
if(this.p.avg){
|
92
|
|
|
r=this.newrow(f);
|
93
|
|
|
for(i=0;i<t.w;i++){
|
94
|
|
|
var j=r.cells[i];
|
|
|
|
|
95
|
|
|
if(this.p.avg.exists(i)){
|
96
|
|
|
var s=c=0, m=t.c[i].format||'';
|
|
|
|
|
97
|
|
|
for(x=0;x<this.t.l;x++){
|
98
|
|
|
if(t.a[x].s){s+=parseFloat(t.r[x].cells[i].innerHTML.replace(/(\$|\,)/g,'')); c++}
|
99
|
|
|
}
|
100
|
|
|
s=decimals(s/c,t.c[i].decimals?t.c[i].decimals:2);
|
101
|
|
|
s=isNaN(s)?'n/a':m=='$'?s=s.currency(t.c[i].decimals):s+m;
|
102
|
|
|
j.innerHTML=s
|
103
|
|
|
}else{j.innerHTML=' '}
|
104
|
|
|
}
|
105
|
|
|
}
|
106
|
|
|
}
|
107
|
|
|
};
|
108
|
|
|
sorter.prototype.newrow=function(p){
|
109
|
|
|
var r=document.createElement('tr'), i=0; p.appendChild(r);
|
110
|
|
|
for(i;i<this.t.w;i++){r.appendChild(document.createElement('td'))}
|
111
|
|
|
return r
|
112
|
|
|
};
|
113
|
|
|
sorter.prototype.alt=function(){
|
114
|
|
|
var t=this.t, i=x=0;
|
|
|
|
|
115
|
|
|
for(i;i<t.l;i++){
|
116
|
|
|
var r=t.r[i];
|
117
|
|
|
if(t.a[i].s){
|
118
|
|
|
r.className=x%2==0?this.p.evenclass:this.p.oddclass; var cells=T$$('td',r);
|
119
|
|
|
for(var z=0;z<t.w;z++){cells[z].className=t.y==z?x%2==0?this.p.evenselclass:this.p.oddselclass:''}
|
120
|
|
|
x++
|
121
|
|
|
}
|
122
|
|
|
if(!t.a[i].s){r.style.display='none'}
|
123
|
|
|
}
|
124
|
|
|
};
|
125
|
|
|
sorter.prototype.page=function(s){
|
126
|
|
|
var t=this.t, i=x=0, l=s+parseInt(this.p.size);
|
|
|
|
|
127
|
|
|
if(this.p.totalrecid){T$(this.p.totalrecid).innerHTML=t.t}
|
128
|
|
|
if(this.p.currentid){T$(this.p.currentid).innerHTML=this.g}
|
129
|
|
|
if(this.p.startingrecid){
|
130
|
|
|
var b=((this.g-1)*this.p.size)+1, m=b+(this.p.size-1); m=m<t.l?m:t.t; m=m<t.t?m:t.t;
|
131
|
|
|
T$(this.p.startingrecid).innerHTML=t.t==0?0:b;; T$(this.p.endingrecid).innerHTML=m
|
132
|
|
|
}
|
133
|
|
|
for(i;i<t.l;i++){var r=t.r[i]; if(t.a[i].s){r.style.display=x>=s&&x<l?'':'none'; x++}else{r.style.display='none'}}
|
134
|
|
|
};
|
135
|
|
|
sorter.prototype.move=function(d,m){
|
136
|
|
|
this.goto(d==1?(m?this.d:this.g+1):(m?1:this.g-1))
|
137
|
|
|
};
|
138
|
|
|
sorter.prototype.goto=function(s){
|
139
|
|
|
if(s<=this.d&&s>0){this.g=s; this.page((s-1)*this.p.size)}
|
140
|
|
|
};
|
141
|
|
|
sorter.prototype.size=function(s){
|
142
|
|
|
var t=this.t;
|
143
|
|
|
if(s){this.p.size=s}
|
144
|
|
|
this.g=1; this.d=Math.ceil(this.t.t/this.p.size);
|
145
|
|
|
if(this.p.navid){T$(this.p.navid).style.display=this.d<2?'none':'block'}
|
146
|
|
|
this.page(0);
|
147
|
|
|
if(this.p.totalid){T$(this.p.totalid).innerHTML=t.t==0?1:this.d}
|
148
|
|
|
if(this.p.pageddid){
|
149
|
|
|
var d=T$(this.p.pageddid), l=this.d+1;
|
|
|
|
|
150
|
|
|
d.setAttribute('onchange',this.n+'.goto(this.value)');
|
151
|
|
|
while(d.hasChildNodes()){d.removeChild(d.firstChild)}
|
152
|
|
|
for(var i=1;i<=this.d;i++){
|
153
|
|
|
var o=document.createElement('option');
|
154
|
|
|
o.value=i; o.innerHTML=i; d.appendChild(o)
|
155
|
|
|
}
|
156
|
|
|
}
|
157
|
|
|
};
|
158
|
|
|
sorter.prototype.showall=function(){
|
159
|
|
|
this.size(this.t.t)
|
160
|
|
|
};
|
161
|
|
|
sorter.prototype.search=function(f){
|
162
|
|
|
var i=x=n=0, k=-1, q=T$(f).value.toLowerCase();
|
|
|
|
|
163
|
|
|
if(this.p.colddid){k=T$(this.p.colddid).value}
|
164
|
|
|
var s=(k==-1)?0:k, e=(k==-1)?this.t.w:parseInt(s)+1;
|
165
|
|
|
for(i;i<this.t.l;i++){
|
166
|
|
|
var r=this.t.r[i], v;
|
167
|
|
|
if(q==''){
|
168
|
|
|
v=1
|
169
|
|
|
}else{
|
170
|
|
|
for(x=s;x<e;x++){
|
171
|
|
|
var b=r.cells[x].innerHTML.toLowerCase();
|
172
|
|
|
if(b.indexOf(q)==-1){v=0}else{v=1; break}
|
173
|
|
|
}
|
174
|
|
|
}
|
175
|
|
|
if(v){n++}
|
176
|
|
|
this.t.a[i].s=v
|
|
|
|
|
177
|
|
|
}
|
178
|
|
|
this.t.t=n;
|
179
|
|
|
if(this.p.paginate){this.size()}
|
180
|
|
|
this.calc(); this.alt()
|
181
|
|
|
};
|
182
|
|
|
sorter.prototype.hover=function(i,d){
|
183
|
|
|
this.t.r[i].id=d?this.p.hoverid:''
|
184
|
|
|
};
|
185
|
|
|
sorter.prototype.set=function(){
|
186
|
|
|
var t=T$(this.id); t.b=T$$('tbody',t)[0]; t.r=t.b.rows; this.t=t
|
187
|
|
|
};
|
188
|
|
|
Array.prototype.exists=function(v){
|
|
|
|
|
189
|
|
|
for(var i=0;i<this.length;i++){if(this[i]==v){return 1}} return 0
|
190
|
|
|
};
|
191
|
|
|
Number.prototype.currency=function(c){
|
|
|
|
|
192
|
|
|
var n=this, d=n.toFixed(c).split('.');
|
193
|
|
|
d[0]=d[0].split('').reverse().join('').replace(/(\d{3})(?=\d)/g,'$1,').split('').reverse().join('');
|
194
|
|
|
return '$'+d.join('.')
|
195
|
|
|
};
|
196
|
|
|
function decimals(n,d){return Math.round(n*Math.pow(10,d))/Math.pow(10,d)};
|
197
|
|
|
function cp(f,c){
|
198
|
|
|
var g,h; f=g=f.v.toLowerCase(); c=h=c.v.toLowerCase();
|
199
|
|
|
var i=parseFloat(f.replace(/(\$|\,)/g,'')), n=parseFloat(c.replace(/(\$|\,)/g,''));
|
200
|
|
|
if(!isNaN(i)&&!isNaN(n)){g=i,h=n}
|
|
|
|
|
201
|
|
|
i=Date.parse(f); n=Date.parse(c);
|
202
|
|
|
if(!isNaN(i)&&!isNaN(n)){g=i; h=n}
|
203
|
|
|
return g>h?1:(g<h?-1:0)
|
204
|
|
|
};
|
205
|
|
|
return{sorter:sorter}
|
206
|
|
|
}(); |